//set working directory 
cd  "C:\Users\johna\Dropbox (Curiel Analytx)\ElectionNightResults\replication_code"
//the above is just the path to the replcation files. The data from here on are 
//hierarchical and nested from this file path. 

//read in the state dta file 
use "data\nyt_states.dta", clear

///get the 60 hour data 


//gen dempct = 100*dem/(dem +rep)
sort state time_num
replace pollsclose = pollsclose[_n-1] if pollsclose == "" & state[_n-1] == state
replace hoursfromclose = ( (time_num - clock(pollsclose,"YMD hms")) )/(1000*60*60) if hoursfromclose == .


preserve
keep if norm_total60 ~= .
keep state norm_total60 norm_dempct60
gen hoursfromclose = 60
gen norm_total = norm_total60
gen norm_dempct = norm_dempct60
set obs 102
replace state = state[_n-51] in 52/l
replace hoursfromclose = 60.01 in 52/l
save "data\scratch\hour60", replace
restore



append using "data\scratch\hour60"
sort state hoursfromclose

twoway (line norm_total hoursfromclose, lcolor(sand%60) lwidth(vthin) cmissing(n) connect(stairstep) ) if hoursfromclose <=60.1 & hoursfromclose > 0, ///
	   ytitle(" ") ylabel(, format(%16.0gc) angle(0)) ///
	   yscale(range(0 100)) ///
	   ylabel(0 10 20 30 40 50 60 70 80 90 100 "100%") ///
	   xtitle("Hours from Close") xlabel(0(4)60) xmtick(0(2)60, grid) ///
	   title("Total votes reported within 60 hours of polls closing") ///
	   subtitle("As a percentage of final vote count")
	   graph export "figures\fig2a.png", replace width(2500)


twoway (line norm_dempct hoursfromclose, lcolor(sand%60) lwidth(vthin) cmissing(n) connect(stairstep)) if hoursfromclose <=60.1 & hoursfromclose > 0, ///
	   ytitle(Percentage points) ylabel(, format(%16.0gc) angle(0)) ///
	   yscale(range(-40 40)) ///
	   ylabel(-40 -30 -20 -10 0 10 20 30 40 "40 points") ///
	   xtitle(Hours from Close) xlabel(0(4)60) xmtick(0(2)60, grid) ///
	   title("Difference between reported Biden" "2-party vote share and final vote share, span")
graph export "figures\fig2b", replace width(2500)

